import turtle
import time

def up_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def show(i):
    turtle.clear()
    turtle.pencolor('black')
    if i < len(lst):
        cur_dict = lst[i]
        up_goto(0, 100)
        turtle.write(cur_dict['q'], False, 'center', ('Arial', 20, 'bold'))
        for i in range(len(cur_dict['options'])):
            up_goto(-200+200*i, -100)
            turtle.write(cur_dict['options'][i], False, 'center', ('Arial', 20, 'bold'))
    else:
        turtle.write('恭喜您答对全部题目', False, 'center', ('Arial', 24, 'normal'))


def func_click(x, y):
    up_goto(0,0)
    global index
    cur_dict = lst[index]
    if y > -100 and y < -70:
        for i in range(len(cur_dict['options'])):
            mid_x = -200 + 200 * i
            if x > mid_x-50 and x < mid_x+50:
                if cur_dict['a'] == cur_dict['options'][i]:
                    turtle.pencolor('green')
                    turtle.write('回答正确', False, 'center', ('Arial', 20, 'bold'))
                    time.sleep(2)
                    index +=1
                    show(index)
                else:
                    turtle.pencolor('red')
                    turtle.write('回答错误', False, 'center', ('Arial', 20, 'bold'))
                    time.sleep(2)
                    turtle.undo()
lst = [
    {
        "q": "整数在python中怎么表示?",
        'options': ['int', 'float', 'str'],
        'a': 'int'
    },
    {
        "q": "字符串在python中怎么表示?",
        'options': ['int', 'float', 'str'],
        'a': 'str'
    }
]
index = 0
# 设置窗口大小
turtle.setup(800,600)
# 设置背景图片 注意图片路径
turtle.bgpic("./bg.gif")
show(index)
turtle.onscreenclick(func_click, 1)
turtle.done()